package logic

import (
	"context"
	"errors"
	"shop/model/mysql"

	"shop/internal/svc"
	"shop/shop"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseShowdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseShowdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseShowdLogic {
	return &HouseShowdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseShowdLogic) HouseShowd(in *shop.HouseShowdRequest) (*shop.HouseShowdResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	var h mysql.House
	if err = h.GetByUserId(); err != nil {
		logx.Error("未查询到此用户")
		return nil, errors.New("未查询到此用户")
	}

	mysql.DB.Find(&h)
	if h.ID != uint(in.Id) {
		logx.Error("未找到此房源")
		return nil, errors.New("未找到此房源")
	}
	return &shop.HouseShowdResponse{
		UserId: int64(h.UserId),
		Title:  h.Title,
		Price:  float32(h.Price),
		Num:    int64(h.Num),
		Info:   h.Info,
		Image:  h.Image,
		ShopId: int64(h.ID),
	}, nil
}
